﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DiskInspector.Model;

namespace DiskInspector
{
    public partial class DOSBootRecordForm : Form
    {
        private readonly DiskPartitionTable DPT;

        private readonly DOSBootRecord DBR;

        public DOSBootRecordForm(Form owner, DiskPartitionTable DPT, DOSBootRecord DBR)
        {
            this.Owner = owner;
            this.DPT = DPT;
            this.DBR = DBR;
            InitializeComponent();
        }

        private void DOSBootRecordForm_Load(object sender, EventArgs e)
        {
            tb_JMPInstruction.Text = DBR.JMPInstruction.ToByteString();
            tb_FileSystemID.Text = new string(DBR.FileSystemID).TrimEnd();
            tb_BytesPerSector.Text = DBR.BytesPerSector.ToString();
            tb_SectorsPerCluster.Text = DBR.SectorsPerCluster.ToString();
            tb_ReservedSectors.Text = DBR.ReservedSectors.ToByteString();
            tb_AlwaysZero1.Text = DBR.AlwaysZero1.ToByteString();
            tb_Unused1.Text = DBR.Unused1.ToByteString();
            tb_MediaDescriptor.Text = DBR.MediaDescriptor.ToByteString();
            tb_AlwaysZero2.Text = DBR.AlwaysZero2.ToByteString();
            tb_SectorsPerTrack.Text = DBR.SectorsPerTrack.ToString();
            tb_Heads.Text = DBR.Heads.ToString();
            tb_HiddenSectors.Text = DBR.HiddenSectors.ToString();
            tb_Unused2.Text = DBR.Unused2.ToByteString();
            tb_Unused3.Text = DBR.Unused3.ToByteString();
            tb_TotalSectors.Text = DBR.TotalSectors.ToString();
            tb_StartClusterNumberMFT.Text = DBR.StartClusterNumberMFT.ToString();
            tb_StartClusterNumberMFTMirr.Text = DBR.StartClusterNumberMFTMirr.ToString();
            tb_FileRecordSizeIndicator.Text = DBR.FileRecordSizeIndicator.ToString();
            tb_Unused4.Text = DBR.Unused4.ToByteString();
            tb_IndexBufferSizeIndicator.Text = DBR.IndexBufferSizeIndicator.ToString();
            tb_Unused5.Text = DBR.Unused5.ToByteString();
            tb_SerialNumber.Text = DBR.SerialNumber.ToByteString();
            tb_CheckSum.Text = DBR.CheckSum.ToString();
            tb_BootstrapLoaderCode.Text = DBR.BootstrapLoaderCode.ToByteString();
            tb_Signature.Text = DBR.Signature.ToByteString();
        }

        private void btn_MFT_Click(object sender, EventArgs e)
        {
            var fileRecordSize = Convert.ToInt32(Math.Pow(2, -1 * DBR.FileRecordSizeIndicator));
            var sectorIndex = DBR.StartClusterNumberMFT * DBR.SectorsPerCluster + DPT.SectorsPrecedingPartition;
            var form = new MainFileTableForm(this, sectorIndex, fileRecordSize, DBR.SectorsPerCluster, DPT.SectorsPrecedingPartition);
            form.ShowDialog();
        }

        private void btn_FullDiskSearch_Click(object sender, EventArgs e)
        {
            var form = new FullDiskSearchForm(DBR.SectorsPerCluster, DPT.SectorsPrecedingPartition, DBR.TotalSectors);
            form.ShowDialog();
        }
    }
}
